'use strict';

/* Controllers */

angular.module('app')
  .controller('AppCtrl', ['$http','$location','$state','$scope', '$translate', '$localStorage', '$window','$modal', '$log',
    function($http, $location,$state, $scope,   $translate,   $localStorage,   $window ,   $modal,  $log) {
      // add 'ie' classes to html
      var isIE = !!navigator.userAgent.match(/MSIE/i);
      isIE && angular.element($window.document.body).addClass('ie');
      isSmartDevice( $window ) && angular.element($window.document.body).addClass('smart');

      // config
      $scope.app = {
        power:{
            'powerId':"",
            'zdr':$.cookie('userName'),
            'zdrId':$.cookie('userId'),
            'powerPrice':false,
            'powerSaleOrder':false,
            'powerSaleWarehousing':false,
            'powerSaleReturn':false,
            'powerPurchasingOrder':false,
            'powerPurchasingWarehousing':false,
            'powerPurchasingReturn':false,
            'powerOthergoodOut':false,
            'powerOthergoodIn':false,
            'powerTransfer':false,
            'powerProductionPlan':false,
            'powerProcessProcess':false,
            'powerInventory':false,
            'powerCommodityAssembly':false,
            'powerMerchandiseDisassembly':false,
            'powerProductMix':false,
            'powerFlow':false,
            'powerUser':false,
            'powerDept':false,
            'powerPower':false,
            'powerCustomer':false,
            'powerSelect':false,
            'powerWarehouse':false,
            'powerGoodsType':false,
            'powerGoods':false,
            'powerInitialEntry':false
        },
        userName:'系统管理员',
        userId:'admin',
        userIdId:'admin',
        name: '永安智能科技',
        version: '1.0.0',
        // for chart colors
        color: {
          primary: '#7266ba',
          info:    '#23b7e5',
          success: '#27c24c',
          warning: '#fad733',
          danger:  '#f05050',
          light:   '#e8eff0',
          dark:    '#3a3f51',
          black:   '#1c2b36'
        },
        settings: {
          themeID: 1,
          showNOne:true,
          showNTwo:false,
          navbarHeaderColor: 'bg-black',
          navbarCollapseColor: 'bg-white-only',
          asideColor: 'bg-black',
          headerFixed: true,
          asideFixed: false,
          asideFolded: false,
          asideDock: false,
          container: false
        },
          logout:function(){
              if($.cookie('userId')==="computer"){
                  $.cookie('userName', null);
                  $.cookie('userId', null);
                  $.cookie('isLOgin', false);
                  $.cookie('userIdId', null);
                  $state.go('access.signin');
              }else{
                  $http.get('/a_yongan/log/logoff', null)
                      .then(function(response) {
                          if(!response.data.logOff){
                              alert("出错！");
                              $state.go('access.signin');
                          }else{
                              $.cookie('userName', null);
                              $.cookie('userId', null);
                              $.cookie('isLOgin', false);
                              $.cookie('userIdId', null);
                              $state.go('access.signin');
                          }
                      }, function(x) {
                          console.log("接口请求出错！")
                      });
              }
          },
          userMessage:function(){
              $state.go('app.users',{docId:$scope.app.userIdId});
          },
          changePassword:function(){
              $state.go('app.changePassword',{docId:$scope.app.userIdId});
          }
      };

      function getPower(){
          if($.cookie('userId')==="null" || $.cookie('userId')===""){
              $.cookie('userName', null);
              $.cookie('userId', null);
              $.cookie('isLOgin', false);
              $state.go('access.signin');
          }else{
              $scope.app.userId = $.cookie('userId');
              $scope.app.userName = $.cookie('userName');
              $scope.app.userIdId = $.cookie('userIdId');
              $http.get('/a_yongan/single/power/'+$.cookie('userId'), null)
                  .then(function(response) {
                      console.log("mainjs,response:",response.data);
                      if(!response.data.isLogin) {
                          $.cookie('userName', null);
                          $.cookie('userId', null);
                          $.cookie('isLOgin', false);
                          $state.go('access.signin');
                      }else{
                          if(response.data.success){
                              $scope.app.power = response.data.data;
                          }else{
                              $scope.app.power = null;
                              alert(response.data.error);
                              $.cookie('userName', null);
                              $.cookie('userId', null);
                              $.cookie('isLOgin', false);
                              $state.go('access.signin');
                          }
                      }
                  }, function(x) {
                      console.log("接口请求出错！");
                  });
          }

      }

      // save settings to local storage
      if ( angular.isDefined($localStorage.settings) ) {
        $scope.app.settings = $localStorage.settings;
      } else {
        $localStorage.settings = $scope.app.settings;
      }
      $scope.$watch('app.settings', function(){
        if( $scope.app.settings.asideDock  &&  $scope.app.settings.asideFixed ){
          // aside dock and fixed must set the header fixed.
          $scope.app.settings.headerFixed = true;
        }
        // save to local storage
        $localStorage.settings = $scope.app.settings;
      }, true);

      // angular translate
      $scope.lang = { isopen: false };
      $scope.langs = {en:'English', de_DE:'German', it_IT:'Italian'};
      $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "English";
      $scope.setLang = function(langKey, $event) {
        // set the current lang
        $scope.selectLang = $scope.langs[langKey];
        // You can change the language during runtime
        $translate.use(langKey);
        $scope.lang.isopen = !$scope.lang.isopen;
      };

      function isSmartDevice( $window )
      {
          // Adapted from http://www.detectmobilebrowsers.com
          var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
          // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
          return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
      }
        getPower();
        // 全局搜索toggle
        // $scope.toggleCustom = function(){
        //     $scope.app.header.whole = $scope.app.header.whole === false ? true : false;
        // }
        // 选责人员
        // $scope.showSelectUser = function(id){
        //     var modalInstance = $modal.open({
        //         templateUrl: 'selectUser.html',
        //         controller: 'selectUserCtrl',
        //         size: 'sm',
        //         resolve: {
        //             items: function () {
        //                 return $scope.message;
        //             }
        //         }
        //     });
        //     modalInstance.result.then(function (selectedItem) {
        //         $scope.app.header.data.userName = selectedItem.name;
        //         $scope.app.header.data.userId = selectedItem.eName;
        //     }, function () {
        //         $log.info('Modal dismissed at: ' + new Date());
        //     });
        // };
        // // 全局搜索数据交互
        // $scope.submit = function(){
        //     let url = "/a_yongan/selectall/select",
        //         data = $scope.app.header.data;
        //     $http.post(url,data).then((res) => {
        //         console.log(res)
        //     })
        // }
        // $scope.showMySearch = false;
        // $scope.showSearch = function(){
        //     $scope.showMySearch = !$scope.showMySearch;
        // };
  }]);